#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDragEnterEvent>
#include <QMimeData>
#include <QDebug>
#include <QList>
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setAcceptDrops(true);
    ui->textEdit->setAcceptDrops(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    qDebug() << "有文件拖动进来" << Qt::endl;
    if(event->mimeData()->hasUrls()){
        event->acceptProposedAction();
    }else{
        event->ignore();
    }
}

void MainWindow::dropEvent(QDropEvent *event)
{
    const auto * mimedata = event->mimeData();
    auto urls = mimedata->urls();
    if(!urls[0].isEmpty())
    {
        QFile file(urls[0].toLocalFile());
        if(!file.open(QIODevice::ReadOnly)){
            return;
        }
        QTextStream in(&file);
        //qDebug()<< "in readall is " << in.readAll() << endl;
        ui->textEdit->setText(in.readAll());
    }
}
